package com.taotao.springbootApi.controller;

import com.taotao.springbootApi.entity.User;
import com.taotao.springbootApi.result.Code;
import com.taotao.springbootApi.result.Result;
import com.taotao.springbootApi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 查询所有
     *
     * @return List<User>
     */
    @GetMapping
    public Result getAll() {
        List<User> list = userService.getAll();
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "查询所有用户信息，查询成功" : "数据查询失败！";
        return new Result(code, list, msg);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return user
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        System.out.println("通过id查询用户信息" + "id = " + id);
        User user = userService.getById(id);
        Integer code = user != null ? Code.GET_OK : Code.GET_ERR;
        String msg = user != null ? "通过用户ID查询，查询成功" : "数据查询失败！";
        return new Result(code, user, msg);
    }

    /**
     * 分页查询<br>
     * 接口路径：/user/page?pageNum=1&pageSize=10<br>
     * limit第一个参数= （pageNum-1)+pageSize
     * <p>
     * 查询一共有多少条数据
     *
     * @param pageNum
     * @param pageSize
     * @return List&lt;User&gt;
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam String username) {
        pageNum = (pageNum - 1);
        List<User> list = userService.selectPage(pageNum, pageSize,username);//分页查询+模糊查询
        Integer total = userService.selectTotal(username);//查询总记录数
        Map<String,Object> res = new HashMap<>();//封装成一个map,创建一个HashMap集合用来存放页面信息，方便调用
        res.put("list",list);
        res.put("total",total);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "分页-模糊查询，查询成功" : "查询失败";
        return new Result(code, res, msg);
    }


    /**
     * 添加记录
     *
     * @param user
     * @return boolean
     */
    @PostMapping
    public Result save(@RequestBody User user) {
        System.out.println("添加用户信息");
        boolean flag = userService.save(user);
        Integer code = flag ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = flag ? "添加成功" :"添加用户信息失败";
        return new Result(code, flag,msg);

    }

    /**
     * 删除记录
     *
     * @param id
     * @return boolean
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = userService.delete(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /**
     * 更新记录
     *
     * @param user
     * @return boolean
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        boolean flag = userService.update(user);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

}
